<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Models\SysMaintenance;

class MaintenanceController extends Controller
{
    /**
     * メンテナンス画面
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *  ECS-58 メンテナンス画面の用意 鄧
     **/
    public function maintenance(Request $request) {
        $sysMaintenance = new SysMaintenance();
        $maitenace =$sysMaintenance->maintenance();
        $nowTime = strtotime("now");
        $maiStrDate = $maitenace[0]->maintenance_start_date;
        if(!empty($maiStrDate)) {
            $maitenace[0]->maintenance_start_date = date('Y-m-d',strtotime($maiStrDate));
        } else {
            $maitenace[0]->maintenance_start_date = date('Y-m-d 00:00', $nowTime);
        }
        $maiEndDate = $maitenace[0]->maintenance_end_date;
        if(!empty($maiEndDate)) {
            $maitenace[0]->maintenance_end_date = date('Y-m-d',strtotime($maiEndDate));
        } else {
            $maitenace[0]->maintenance_end_date = date('Y-m-d 23:45', $nowTime);
        }
        $maiStrtime = $maitenace[0]->maintenance_start_time;
        $maitenace[0]->maintenance_start_time = date('H:i',strtotime($maiStrtime));
        $maiEndtime = $maitenace[0]->maintenance_end_time;
        $maitenace[0]->maintenance_end_time = date('H:i',strtotime($maiEndtime));
        return $this->successWithData($maitenace);
    }
    /**
     * メンテナンス画面
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *  ECS-58 メンテナンス画面の用意 鄧
     **/
    public function maintenanceGuset(Request $request) {
        $sysMaintenance = new SysMaintenance();
        $maitenace =$sysMaintenance->maintenance();
        $nowTime = strtotime("now");
        $startTime = date('Y-m-d '.$maitenace[0]->maintenance_start_time,strtotime($maitenace[0]->maintenance_start_date));
        $EndTime = date('Y-m-d '.$maitenace[0]->maintenance_end_time,strtotime($maitenace[0]->maintenance_end_date));
//        return $maitenace;
        if($maitenace[0]->maintenance === 1){
            if ($nowTime >= strtotime($startTime) && $nowTime <= strtotime($EndTime)) {
                $maitenace[0]->maintenance = 1;
            } else {
                $maitenace[0]->maintenance = 0;
            }
        } else if ($maitenace[0]->maintenance === 0){
            $maitenace[0]->maintenance = 0;
        }
        return $this->successWithData($maitenace);
    }
    /**
     * メンテナンス画面
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *  ECS-58 メンテナンス画面の用意 鄧
     **/
    public function maintenanceUpDate(Request $request) {
        $sysMaintenance = new SysMaintenance();
       $maintenanceUpDate= $sysMaintenance->maintenanceUpData($request);
       return $this->successWithData($maintenanceUpDate);

    }
}
